﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace qf_cms.admin
{
	public partial class do_LoadColumn : SessionPage
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			string id = Request.QueryString["id"] + "";

			string dataString = GetJsonData(id);

			Response.Write(dataString);
		}

		public string GetJsonData(string parentID)
		{
			#region dynaTree
			/**
			 * res = '[' 
			 * res += '{ "title": "Node 1", "key": "k1", "isLazy": true },' 
			 * res += '{ "title": "Node 2", "key": "k2", "isLazy": true },' 
			 * res += '{ "title": "Node 3", "key": "k3", "isLazy": true }' # no trailing "," at the last line
			 * res += ']'
			 */
			try
			{
				string result = "";

				System.Text.StringBuilder sb = new System.Text.StringBuilder();

				LogicLayer mgr = new LogicLayer();

				List<CMS_Columns> lstColumn = mgr.ListColumns(parentID);

				sb.Append("[");

				if (lstColumn != null)
				{
					foreach (CMS_Columns col in lstColumn)
					{
						sb.Append("{ \"title\": \"" + col.ColumnName + "\", \"key\": \"" + col.ColumnID + "\", \"url\": \"columnGeneral.aspx?id=" + col.ColumnID + "\", \"isFolder\" : \"true\" ");
						if (mgr.HasChildColumn(col.ColumnID))
							sb.Append(",\"isLazy\": true ");

						sb.Append("},");
					}
					result = sb.ToString();
					if (result.EndsWith(","))
						result = result.Substring(0, result.Length - 1);

					result += "]";
				}

				if (parentID == "")
				{
					result = "[{ \"title\": \"网站栏目\", \"key\": \"0\", \"url\": \"columnGeneral.aspx\", \"isFolder\": true, \"isLazy\": true , \"expand\":true, \"children\": " + result + "}";
					result += ",{\"title\": \"设置\", \"key\": \"setting\", \"isFolder\": true, \"expand\":true, \"children\": [{\"title\": \"修改密码\", \"key\": \"chgpwd\", \"url\": \"changepwd.aspx\"}]}";
					result += "]";
				}

				return result;  // 输出JSON字符串
			}
			catch (Exception ex)
			{
				//Logger.Write(ex.Message);
				return ex.Message;
			}
			#endregion
		}
	}
}